<?php
/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/3/3
 * Time: 15:24
 */

namespace App\Models;

use App\Http\Helper\UrlSchemaHelper;
use App\Http\Helper\YunXinServerAPI;
use App\Models\Concerns\CommentTraits;

/**
 * Class Dynamic
 * @property integer id
 * @property integer user_id
 * @property string content
 * @property integer view_count
 */
class Dynamic extends ApplicationModel
{
    use CommentTraits;

    protected $fillable = [
        'user_id',
        'content',
        'view_count',
    ];

    protected static function boot()
    {
        parent::boot();
        static::created(function (Dynamic $dynamic) {
            $dynamic->user->push_follow('dynamic', $dynamic->id, '用户' . $dynamic->user->name . '刚发布了新的动态，快来看看吧！');
        });
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function like_count()
    {
        return $this->likes()->count();
    }

    public function likes()
    {
        return $this->hasMany(Like::class, 'type_id')->where('type', $this->model_name());
    }

    public function purchases()
    {
        return $this->hasMany(Purchase::class, 'product_id')->where('is_payed', Purchase::PAY_SUCCESS)->where('type', $this->model_name());
    }

    public function pictures()
    {
        return $this->hasMany(Picture::class, 'type_id')->where('type', $this->model_name())->orderBy('created_at', 'desc')->limit(9);
    }

    public function send_msg($content, $url)
    {
        //发送推送
        $message = new Message();
        $message->user_id = $this->user_id;
        $message->content = $content;
        $message->icon = Message::ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->save();

        $message->pushMsg($this->user_id, $content, "{'url':'" . $url . "'}");
    }

    public function share_content()
    {
        $do_main = config('constants.DO_MAIN');
        $arr = array(
            'qqShare' => array('title' => '没想到Ta是这样的大神', 'content' => '“' . $this->user->name . '”在CC星球发布了个人动态，没想到Ta是这样的大神，点击查看', 'url' => $do_main . '/app/download.html', 'cover' => $this->user->avatar),
            'wxShare' => array('title' => '没想到Ta是这样的大神', 'content' => '“' . $this->user->name . '”在CC星球发布了个人动态，没想到Ta是这样的大神，点击查看', 'url' => $do_main . '/app/download.html', 'cover' => $this->user->avatar),
            'wxQuan' => array('title' => '没想到Ta是这样的大神', 'content' => '“' . $this->user->name . '”在CC星球发布了个人动态，没想到Ta是这样的大神，点击查看', 'url' => $do_main . '/app/download.html', 'cover' => $this->user->avatar),
            'wbShare' => array('title' => '没想到Ta是这样的大神', 'content' => '“' . $this->user->name . '”在CC星球发布了个人动态，没想到Ta是这样的大神，点击查看', 'url' => $do_main . '/app/download.html', 'cover' => $this->user->avatar),
            'qqZone' => array('title' => '没想到Ta是这样的大神', 'content' => '“' . $this->user->name . '”在CC星球发布了个人动态，没想到Ta是这样的大神，点击查看', 'url' => $do_main . '/app/download.html', 'cover' => $this->user->avatar),
        );

        return $arr;
    }

    public function tranTime($time) {
        $time = strtotime($time);
        $rtime = date("m-d H:i",$time);
        $htime = date("H:i",$time);
        $time = time() - $time;
        if ($time < 60) {
            $str = '刚刚';
        }
        elseif ($time < 60 * 60) {
            $min = floor($time/60);
            $str = $min.'分钟前';
        }
        elseif ($time < 60 * 60 * 24) {
            $h = floor($time/(60*60));
            $str = $h.'小时前 '.$htime;
        }
        elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor($time/(60*60*24));
            if($d==1)
                $str = '昨天 '.$rtime;
            else
                $str = '前天 '.$rtime;
        }
        else {
            $str = $rtime;
        }
        return $str;
    }
}
